package com.rongji.dfish.framework.plugin.work.service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  

import com.google.gson.JsonArray;
import com.google.gson.JsonParser;




   
//解析多层map
 public class RegexUtil {  

     //与解析策略相匹配的正则表达式  

     //private static final String REGULAR_IMG_HTML = "<img +?src=\"(.+?)\"";  

     private static final String REGULAR_JSON_ITEM_NAME = "\"([^\\\" ]+?)\":";  

     //private static final String REGULAR_JSON_ARRAY_NAME = ", *?\" *?([a-zA-Z0-9]*?) *?\" *?: *?\\[ *?\\{";  
     

         //公用方法解析，将字符窜传入即可  

     public static Object regex(final RegexPolicy policy, final String data) {  

         switch (policy) {  

         case Json:  

             return regexJson(data);  

         case Image:  

             break;  

         default:  

             break;  

         }  

         return null;  

     }  
     public enum RegexPolicy {  
    	      Json("Json"),  
    	      Image("ImageFromHtml");      
    	      private String value;  
    	      RegexPolicy (String value) {  
    	          this.value = value;  
    	      }  
    	      public String toString() {  
    	          return value;  
    	      }  
    	  }  


     private static Object regexJson(final String jsonStr) {  

         if (jsonStr == null) throw new NullPointerException("JsonString shouldn't be null");  
         final JsonParser jsonparer = new JsonParser();//初始化解析json格式的对象           
         try {  

             if (isJsonObject(jsonStr)) {  

                 final Pattern pattern = Pattern.compile(REGULAR_JSON_ITEM_NAME);  

                 final Matcher matcher = pattern.matcher(jsonStr);  

                 final TreeMap<String, Object> map = new TreeMap<String, Object>();  

                 for ( ; matcher.find(); ) {  

                     String groupName = matcher.group(1);  
                     Object obj = jsonparer.parse(jsonStr).getAsJsonObject().get(groupName);

                     if (isJsonObject(obj.toString()) || isJsonArray(obj.toString())) {  

                         matcher.region(matcher.end() + obj.toString().replace("\\", "").length(), matcher.regionEnd());  

                         map.put(groupName, regexJson(obj.toString()));   
                     }
                     else {  

                         map.put(groupName, jsonparer.parse(jsonStr).getAsJsonObject().get(groupName).getAsString());  

                     }  

                 }  

                 return map;  

             } else if (isJsonArray(jsonStr)) {  

                 List<Object> list = new ArrayList<Object>();  

                 //JSONArray jsonArray = new JSONArray(jsonStr); 
                 JsonArray obj = jsonparer.parse(jsonStr).getAsJsonArray();

                 for (int i = 0; i < obj.size(); i++) {  

                     String str = obj.get(i).getAsString();  

                     list.add(regexJson(str));  

                 }  

                 return list;  

             }   

         } catch (Exception e) {        
         }  

         return jsonStr;  

     }  

        

     /**  

      * To determine whether a string is JsonObject {@link org.json.JSONObject}  

      * @param jsonStr {@link java.lang.String}  

      * @return boolean  

      */ 

     private static boolean isJsonObject(final String jsonStr) {  

         if (jsonStr == null) return false;  

         return Pattern.matches("^\\{.*\\}$", jsonStr.trim());  

     }  

        

     /**  

      * To determine whether a string is JsonArray {@link org.json.JSONArray};  

      * @param jsonStr {@link java.lang.String}  

      * @return boolean  

      */ 

     private static boolean isJsonArray(final String jsonStr) {  

         if (jsonStr == null) return false;  

         return Pattern.matches("^\\[.*\\]$", jsonStr.trim());  

     }  

 }  